<?php
	function f260($sql){
		//data handle instance
		//set request
		//define format
		//set format
		//define query string
		//process time
		//process fitler
		$dataHandle = new DataHandle();
		$dataHandle->setRequest("260");

		$action = getValue($_GET['action']);

		if ($action == "getItems"){
			$format = array(
				"date" => "datem/d/Y",
				"product_id_from" => "numb6",
				"product_id_to" => "numb6",
				"start_time" => "dateH:i",
				"end_time" => "dateH:i",
				"modified_date" => "datem/d/Y",
			);
			$dataHandle->setFormat($format);
			$error = $dataHandle->processTime($startTime, $endTime);
			if($error == ""){
				$filter = $dataHandle->processFilters($startTime, $endTime, "lin_Technical_Changeovers","start_time");
				$query = getJSONQuery($startTime, $endTime, $filter);
				print($dataHandle->printJSON($sql, $query));
			}
			else
				print($error);
		}
		else if($action == "getYears"){
			$query = getYears();
			print($dataHandle->printJSON($sql, $query));
		}
		else if($action == "getLines"){
			$query = getLines();
			print($dataHandle->printJSON($sql, $query));
		}
		else{
			$temp = array();
			$temp['error'] = "260: Bad request";
			print(json_encode($temp));
		}

		unset($dataHandle);

	}

	function getJSONQuery(&$startTime, &$endTime, &$filter){

		$queryString = "
		select
			date,
			line_id,
			product_id_from,
			product_id_to,
			minutes,
			start_time,
			end_time,
			modified_by,
			modified_date
		from lin_Technical_Changeovers";

		$queryString = $queryString.$filter;

		$oderByFiled = getValue($_GET['sortItem']);
		$oderByValue = getValue($_GET['ascDesc']);

		if($oderByFiled !== "" and $oderByValue !== "")
			$queryString = $queryString." order by ".$oderByFiled." ".$oderByValue;

		return $queryString;
	}
	function getYears(){
		$queryString = 
			"SELECT DISTINCT 
				DATEPART(year, start_time) AS availableYear 
			FROM lin_Technical_Changeovers 
			ORDER BY availableYear desc";
		return $queryString;
	}
	function getLines(){
		$queryString = 
			"SELECT DISTINCT 
				line_id AS availableLine 
			FROM lin_Technical_Changeovers 
			ORDER BY availableLine asc";
		return $queryString;
	}
?>